// pages/person/person.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    avatar: '',
    message: '',
    value: '',
    num: {},
    hasUserInfo: true,
    canIUseGetUserProfile: false,
    show: false,
    max: 100, //限制最大输入字符数
    worksShow: true,
    loveShow: false,
    collectShow: false,
    likeData: [],
    collectData: [],
    myData: [],
    temp: false,
    list: [{
        name: '最近观看',
        add: 'todo-list-o',
        color: '#7D88FC',
        url: "/pages/history/history"
      },
      {
        name: '消息',
        add: 'chat-o',
        color: '#F56D6B',
        url: '/pages/message/message'
      },
      {
        name: '邀请好友',
        add: 'share',
        color: '#60A1F9',
        url: false
      },
      {
        name: '修改资料',
        add: 'setting-o',
        color: '#FEB350',
        url: '/pages/set/set'
      },
      {
        name: '关注',
        add: 'friends-o',
        color: '#7D88FC',
        url: '/pages/concern/concern'
      },
      {
        name: '粉丝',
        add: 'like',
        color: '#F56D6B',
        url: '/pages/fan/fan'
      },
      {
        name: '常见问题',
        add: 'question-o',
        color: '#60A1F9',
        url: '/pages/question/question'
      },
      {
        name: '反馈',
        add: 'records',
        color: '#FEB350',
        url: '/pages/feedback/feedback'
      }
    ],
    showShare: false,
    options: [{
        name: '微信',
        icon: 'wechat',
        openType: 'share'
      },
      {
        name: '微博',
        icon: 'weibo'
      },
      {
        name: '复制链接',
        icon: 'link'
      },
      {
        name: '分享海报',
        icon: 'poster'
      },
      {
        name: '二维码',
        icon: 'qrcode'
      },
    ]
  },
  navi(e) {
    let index = e.currentTarget.dataset.a
    if (!this.data.list[index].url) {
      console.log('aa')
      this.setData({
        showShare: !this.data.showShare
      })
      return;
    }
    wx.navigateTo({
      url: this.data.list[index].url + "?id=" + index,
    })
  },
  onClick(event) {
    this.setData({
      showShare: true
    });
  },

  onClose() {
    this.setData({
      showShare: false
    });
  },

  onSelect(event) {
    Toast(event.detail.name);
    this.onClose();
  },
  aaa() {
    wx.navigateBack({
      delta: 1
    })
  },

  // 简介
  getValueLength: function (e) {
    let value = e.detail.value
    let len = parseInt(value.length)
    if (len > 100) return;
    this.setData({
      value: value,
      currentWordNumber: len //当前字数 
    })
  },
  onClickShow() {
    if (this.data.message) {
      this.setData({
        show: false
      });
    } else {
      this.setData({
        show: true
      });
    }
  },

  onClickHide() {
    wx.request({
      url: 'http://localhost:1234/person/intro',
      method: "POST",
      header: {
        'content-type': "application/json"
      },
      data: JSON.stringify({
        value: this.data.value,
        userid: wx.getStorageSync('userid')
      }),
      success: res => {
        console.log(res.data);
        this.setData({
          message: this.data.value,
          show: false
        })
      },

      fail: err => {
        console.log(err)
      }
    })
  },


  // 作品
  worksList: function () {
    wx.request({
      url: 'http://localhost:1234/person/works?userid=' + wx.getStorageSync('userid'),
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data)
        this.setData({
          myData: res.data
        })
      },
      fail: error => {
        console.log(error);
      }
    })
    this.setData({
      worksShow: true,
      loveShow: false,
      collectShow: false
    })
  },
  // 喜欢
  loveList: function () {
    wx.request({
      url: 'http://localhost:1234/user/star/like?userid=' + wx.getStorageSync('userid'),
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data)
        this.setData({
          worksShow: false,
          loveShow: true,
          collectShow: false,
          likeData: res.data
        })
      },
      fail: error => {
        console.log(error);
      }
    })
  },
  // 收藏
  collectList: function () {
    wx.request({
      url: 'http://localhost:1234/user/star/collect?userid=' + wx.getStorageSync('userid'),
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data)

        this.setData({
          worksShow: false,
          loveShow: false,
          collectShow: true,
          collectData: res.data
        })
      },
      fail: error => {
        console.log(error);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //作品
    let userid = wx.getStorageSync('userid');
    wx.request({
      url: 'http://localhost:1234/works/mine?userid=' + userid.substring(1),
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        this.setData({
          myData: res.data
        })
      },

      fail: err => {
        console.log(err);
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 用户信息
    wx.request({
      url: 'http://localhost:1234/person?userid=' + wx.getStorageSync('userid'),
      method: "GET",
      header: {
        'content-type': "application/json"
      },
      success: res => {
        console.log(res.data)
        let per = false
        if (res.data.perface != '') {
          per = 'http://localhost:1234/public/material/' + res.data.perface
        }
        this.setData({
          userInfo: res.data,
          message: res.data.intro,
          temp: per
        })
        let userInfo = wx.getStorageSync('userInfo');
        if (userInfo.avatarUrl.includes('//')) {
          console.log('ba');
          this.setData({
            avatar: userInfo.avatarUrl
          })
        } else {
          console.log('aa')
          this.setData({
            avatar: 'http://localhost:1234/public/material/' + res.data.avatar
          })
        }
      },
      fail: err => {
        console.log(err);
      }
    });
    this.worksList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})